#pragma once

#include <string>
#include <map>
#include <memory>

#include <uv.h>

#include "loop.h"
#include "connection_base.h"

namespace anb { namespace net {

class server_base : public uvo_looped, public connection_base::listener {
public:
    server_base(loop::ptr loop);
    virtual ~server_base();

    int start(const struct sockaddr_in* addr);
    virtual void stop();

protected:
	virtual void connection_stopped(connection_base* connection);
	virtual connection_base* create_connection(uv_tcp_t* tcp);

protected:
    static void _accepted(uv_stream_t* server_base, int status);
    void accepted(int err);

private:
    uv_tcp_t* m_tcp;

	typedef std::map<std::string, connection_base::ptr> connections;
	connections m_connections; // TODO thread safe
};


} }